package com.airsystem.pos.bengkel.controller.dialog;

import java.math.BigDecimal;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.entity.JenisSparepart;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.Sparepart;
import com.airsystem.pos.bengkel.interfaces.ILookup;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.ISystem;
import com.airsystem.pos.bengkel.model.dialog.MasterSparepartDialogModel;
import com.airsystem.pos.bengkel.service.SparepartServices;
import com.airsystem.pos.bengkel.util.Preconditions;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterSparepartDialogController extends MasterSparepartDialogModel implements ILookup, ISystem {
	private static final Logger LOG = Logger.getLogger(MasterSparepartDialogController.class.getSimpleName());

	private IServices<DetailSparepart> service;

	public MasterSparepartDialogController() {
		super();

		service = new SparepartServices();
	}

	public void setup() {
		TableFrame.deleteTable(tableData, tableModel);
	}

	public void get() {
		String merk = tableData.getValueAt(row, 0).toString();
		String subMerk = tableData.getValueAt(row, 1).toString();
		String merkKendaraan = tableData.getValueAt(row, 2).toString();
		String subMerkKendaraan = tableData.getValueAt(row, 3).toString();
		String namaJenisSparepart = tableData.getValueAt(row, 4).toString();
		String hargaSatuan = tableData.getValueAt(row, 5).toString().replace(".00", "");
		String stok = tableData.getValueAt(row, 6).toString();
		String idJenisSparepart = tableData.getValueAt(row, 7).toString();

		Sparepart sp = new Sparepart();
		sp.setMerk(merk);

		JenisSparepart jenissp = new JenisSparepart();
		jenissp.setId(Long.parseLong(idJenisSparepart));
		jenissp.setNama(namaJenisSparepart);

		Kendaraan kendaraan = new Kendaraan();
		kendaraan.setMerk(merkKendaraan);

		DetailKendaraan detailKendaraan = new DetailKendaraan();
		detailKendaraan.setSub_merk(subMerkKendaraan);
		detailKendaraan.setKendaraan(kendaraan);

		DetailSparepart detailSparepart = new DetailSparepart();
		detailSparepart.setSub_merk(subMerk);
		detailSparepart.setHarga_satuan(new BigDecimal(hargaSatuan));
		detailSparepart.setStok(Long.parseLong(stok));
		detailSparepart.setSparepart(sp);
		detailSparepart.setDetail_kendaraan(detailKendaraan);
		detailSparepart.setJenis_sparepart(jenissp);

		setDetailSparepart(detailSparepart);
	}

	public void reset() {
		searchField.setText("");
		searchCombo.requestFocus();
	}

	public void category() {
		throw new UnsupportedOperationException("Not Implemented.");
	}

	public void search() {
		try {
			Preconditions.checkState(searchCombo.getSelectedIndex() == 0, "Combobox not chosen.");
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(this, "Silahkan pilih data!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();

			return;
		}

		try {
			setup();
			Iterable<DetailSparepart> layananTypes = service.select(getSearchData());

			for (DetailSparepart item : layananTypes) {
				Vector<String> vector = new Vector<String>();
				vector.add(item.getSparepart().getMerk());
				vector.add(item.getSub_merk());
				vector.add(item.getDetail_kendaraan().getKendaraan().getMerk());
				vector.add(item.getDetail_kendaraan().getSub_merk());
				vector.add(item.getJenis_sparepart().getNama());
				vector.add(item.getHargaSatuan_s());
				vector.add(item.getStok_s());
				vector.add(item.getJenis_sparepart().getId_s());

				tableModel.addRow(vector);
			}

			tableData.setModel(tableModel);
		} catch (IllegalArgumentException e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Terdapat kesalahan pada input!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void load() {
		try {
			setup();
			Iterable<DetailSparepart> detailSpareparts = service.select(new DetailSparepart());

			for (DetailSparepart item : detailSpareparts) {
				Vector<String> vector = new Vector<String>();
				vector.add(item.getSparepart().getMerk());
				vector.add(item.getSub_merk());
				vector.add(item.getDetail_kendaraan().getKendaraan().getMerk());
				vector.add(item.getDetail_kendaraan().getSub_merk());
				vector.add(item.getJenis_sparepart().getNama());
				vector.add(item.getHargaSatuan_s());
				vector.add(item.getStok_s());
				vector.add(item.getJenis_sparepart().getId_s());

				tableModel.addRow(vector);
			}
			tableData.setModel(tableModel);

			this.setVisible(true);
		} catch (Exception e) {
			this.setVisible(false);
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}